/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.client.widgets.wizard;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * 
 * @author Chris Worley
 *
 */
public class Wizard {
  
  /** wizard pages */
  private List wizardPages;

  /** reference to dialog container */
  private WizardDialog wizardDialog;

  /**
   * Default constructor
   * 
   */
  public Wizard() {
    wizardPages = new ArrayList();
    initializePages();
  }

  /**
   * determine if the wizard can be finished
   * 
   * @return
   */
  public boolean canFinish() {
    for (Iterator iter = wizardPages.iterator(); iter.hasNext();) {
      WizardPage page = (WizardPage) iter.next();
      if (!page.isComplete()) {
        return false;
      }
    }
    return true;
  }

  /**
   * Override and provide implementation to add pages to the wizard
   * 
   */
  protected void initializePages() {
    // do nothing
  }

  /**
   * add wizard page to list
   * 
   * @param page
   */
  public void addPage(WizardPage page) {
    page.setWizard(this);
    wizardPages.add(page);
  }

  /**
   * return current wizard page
   * 
   * @return
   */
  public WizardPage getCurrentPage() {
    return wizardDialog.getCurrentWizardPage();
  }

  /**
   * return next pae to be displayed
   * 
   * @return
   */
  protected WizardPage getNextPage() {
    int nextIndex = wizardPages.indexOf(wizardDialog.getCurrentWizardPage()) + 1;

    // if next index is out of bounds return null
    if (nextIndex >= wizardPages.size()) {
      return null;
    }

    return (WizardPage) wizardPages.get(nextIndex);
  }

  /**
   * return wizard page list
   */
  protected List getWizardPages() {
    return wizardPages;
  }

  /**
   * return previous page to be display
   * 
   * @return
   */
  protected WizardPage getPreviousPage() {
    int previousIndex = wizardPages.indexOf(wizardDialog.getCurrentWizardPage()) - 1;

    // if page index is less than zero return null
    if (previousIndex < 0) {
      return null;
    }

    return (WizardPage) wizardPages.get(previousIndex);
  }

  /**
   * return first page
   * 
   * @return
   */
  public WizardPage getFirstPage() {
    return (WizardPage) wizardPages.get(0);
  }

  /**
   * 
   * getter for wizardDialog
   * 
   * @return the wizardDialog
   */
  public WizardDialog getWizardDialog() {
    return wizardDialog;
  }

  /**
   * finish pressed
   * 
   * @return true if wizard can close, otherwise false
   */
  protected boolean performFinish() {
    return true;
  }

  /**
   * setter for wizard dialog
   * 
   * @param wizardDialog
   */
  protected void setWizardDialog(WizardDialog wizardDialog) {
    this.wizardDialog = wizardDialog;
  }

}
